package com.example.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.QueryPageParam;
import com.example.common.Result;
import com.example.pojo.User;
import com.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zzw
 * @since 2023-10-28
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @PostMapping("/login")
    public Result login(@RequestBody User user) {
        List list = userService.lambdaQuery()
                .eq(User::getAccount, user.getAccount())
                .eq(User::getPassword, user.getPassword()).list();
        if(list.size() > 0) {
            User user1 = (User)list.get(0);
            HashMap res = new HashMap();
            res.put("user", user1);
            return Result.success(res);
        }
        return Result.fall();
    }

    @GetMapping("/findByAccount")
    public Result findByNo(@RequestParam String account) {
        List<User> list = userService.lambdaQuery().eq(User::getAccount, account).list();
        return list.size() > 0 ? Result.success(list) : Result.fall();
    }

    @PostMapping("/listPage")
    public Result listPage(@RequestBody QueryPageParam query) {

        HashMap param = query.getParam();
        String name = (String)param.get("name");
        String sex = (String)param.get("sex");
        String dept = (String)param.get("dept");
        String roleId = (String)param.get("roleId");

        Page<User> page = new Page<>();
        page.setCurrent(query.getPageNum());
        page.setSize(query.getPageSize());
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if(StringUtils.isNotBlank(name) && !"null".equals(name)) {
            lambdaQueryWrapper.like(User::getName, name);
        }
        if(StringUtils.isNotBlank(sex) && !"null".equals(sex)) {
            lambdaQueryWrapper.like(User::getSex, sex);
        }
        if(StringUtils.isNotBlank(roleId) && !"null".equals(roleId)) {
            lambdaQueryWrapper.like(User::getRoleId, roleId);
        }
        if(StringUtils.isNotBlank(dept) && !"null".equals(dept)) {
            lambdaQueryWrapper.like(User::getDept, dept);
        }
        IPage result = userService.page(page, lambdaQueryWrapper);
        return Result.success(result.getRecords(), result.getTotal());
    }

    @GetMapping("/delete")
    public Result delete(@RequestParam String id) {
        return userService.removeById(id) ? Result.success() : Result.fall();
    }

    //增加
    @PostMapping("/save")
    public Result save(@RequestBody User user) {
        return userService.save(user) ? Result.success() : Result.fall();
    }

    @PostMapping("/update")
    public Result update(@RequestBody User user) {
        return userService.updateById(user) ? Result.success() : Result.fall();
    }
}
